﻿using System;
using System.Text;
using System.Net;
using System.IO;

/// <summary>
///HTTP 的摘要说明
/// </summary>
public class HTTP
{
    /// <summary>
    /// http的基本post方法
    /// </summary>
    /// <param name="url">URL地址</param>
    /// <param name="postData">post的数据内容</param>
    /// <returns>服务器返回的数据</returns>
    public static string Post(string url, string postData)
    {
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        try
        {
            ServicePointManager.DefaultConnectionLimit = 300; //HTTPWebRequest有一个默认连接数限制

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout = 30000;
            request.Method = "POST";
            request.ContentLength = byteArray.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteArray, 0, byteArray.Length);
            requestStream.Close();

            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
            String sResult = reader.ReadToEnd();
            reader.Close();
            return sResult;
        }
        catch
        {
            return "";
        }
    }
}